<template>
  <div class="app-container">
    <el-form ref="formRef" :model="model" :rules="rules" class="" label-width="120px">
      <el-form-item label="旧密码" prop="password_old" style="width: 40%">
        <el-input v-model="model.password_old" type="password" placeholder="请输入旧密码" autocomplete="off" clearable show-password/>
      </el-form-item>
      <el-form-item label="新密码" prop="password_new" style="width: 40%">
        <el-input v-model="model.password_new" type="password" placeholder="请输入新密码" autocomplete="off" clearable show-password/>
      </el-form-item>
      <el-form-item>
        <el-button @click="reset">重置</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {resetPwd} from '@/api/admin/admin'

export default {
  name: 'AdminResetPwd',
  components: {},
  data() {
    return {
      loading: false,
      model: {
        password_old: '',
        password_new: ''
      },
      rules: {
        password_old: [{required: true, message: '请输入旧密码', trigger: 'blur'}],
        password_new: [
          {required: true, message: '请输入新密码', trigger: 'blur'},
          {min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    reset() {
      this.$refs['formRef'].resetFields()
    },
    submit() {
      this.$refs['formRef'].validate(valid => {
        if (valid) {
          this.loading = true
          resetPwd(this.model).then(res => {
            this.reset()
            this.loading = false
            this.$message.success(res.msg)
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
